const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const cors = require('cors');

const config  = require('./config');

// 创建Express应用实例
const app = express();

// 使用body-parser中间件
app.use(bodyParser.json());
app.use(cors());
// 处理POST请求
app.post('/app/ai', (req, res) => {

  // 响应数据
  // res.setHeader('Content-Type', 'application/json');
  let data ;
  axios.post('https://llama.family/serverApi/llama/chat/ask-question', {
    modelCode: 'b9e12223780c49838919338cb64484ea',
    question: "你叫小仙1号机器人请用简体中文回复我的问题"+req.body.question,
    messages: [],
  }, config)
    .then((response) => {
      const fruits = response.data.split('data:');
     console.log('用户提问:', req.body);

      data =JSON.parse(fruits[fruits.length - 1]);
   
      res.json(data);
    })
    .catch((error) => {
      console.error('Error:', error);
    });

});

// 监听3000端口
app.listen(3000, () => {
  console.log('服务启动本地地址:http://127.0.0.1:3000');
});